package com.ruoyi.job.service;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.job.domain.SysUser;
import com.ruoyi.job.mapper.SysUserMapper;
import com.ruoyi.job.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-08
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询用户列表 包含供给方和需求方
     *
     * @param sysUser 用户
     * @return 用户集合
     */
    @Override
    public List<SysUser> selectSysUserListAll(SysUser sysUser) {
        return sysUserMapper.selectSysUserListAll(sysUser);
    }

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public SysUser selectSysUserByUserId(Long userId) {
        return sysUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param sysUser 用户
     * @return 用户
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser) {
        /*校验手机号*/
        if (sysUserMapper.checkPhoneNumber(sysUser) == 0) {
            sysUser.setCreateTime(DateUtils.getNowDate());
            sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
            sysUser.setUserName(sysUser.getPhonenumber());
            int i = sysUserMapper.insertSysUser(sysUser);
            switch (sysUser.getUserType()) {
                case "01":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 100);
                    break;
                case "02":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 101);
                    break;
                case "03":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 102);
                    break;
            }
            return i;
        } else {
            throw new RuntimeException("手机号已存在");
        }
    }

    /**
     * 修改用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser) {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        SysUser user = sysUserMapper.selectSysUserByUserId(sysUser.getUserId());
        /*检查手机号是否修改*/
        if (sysUser.getPhonenumber() != null && user.getPhonenumber().equals(sysUser.getPhonenumber())) {
            return sysUserMapper.updateSysUser(sysUser);
        }

        /*校验手机号*/
        if (sysUser.getPhonenumber() != null) {
            if (sysUserMapper.checkPhoneNumber(sysUser) == 0) {
                sysUser.setUserName(sysUser.getPhonenumber());
            } else {
                throw new RuntimeException("手机号已存在");
            }
        }

        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserIds(Long[] userIds) {
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserId(Long userId) {
        return sysUserMapper.deleteSysUserByUserId(userId);
    }
}
